//
//  MPOtherHomeCommentCell.m
//  MPOtherHomeDemo
//
//  Created by YC on 2018/9/6.
//  Copyright © 2018年 yc. All rights reserved.
//

#import "MPOtherHomeCommentCell.h"
#import <Masonry/Masonry.h>
#import "MPHeader.h"

@interface MPOtherHomeCommentCell ()

/// 左侧线条view
@property (nonatomic, strong) UIView *lineView;

/// 左侧线条上的icon
@property (nonatomic, strong) UIImageView *lineImageView;

/// 时间label
@property (nonatomic, strong) UILabel *timeLabel;

/// 内容label
@property (nonatomic, strong) UILabel *contentLabel;

/// 地点icon
@property (nonatomic, strong) UIImageView *locationImageView;

/// 地点label
@property (nonatomic, strong) UILabel *locationLabel;

/// 评论btn
@property (nonatomic, strong) UIButton *commentBtn;

/// 点赞btn
@property (nonatomic, strong) UIButton *praiseBtn;

/// 图片展示view
@property (nonatomic, strong) UIView *imagesView;

/// 图片数组
@property (nonatomic, strong) NSArray<NSString *> *images;


/// 图片宽度
@property (nonatomic, assign) float imageWidth;

@end

@implementation MPOtherHomeCommentCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self) {
        [self createUI];
    }
    return self;
}

- (void)createUI {
    [self.contentView addSubview:self.lineView];
    [self.contentView addSubview:self.timeLabel];
    [self.contentView addSubview:self.contentLabel];
    [self.contentView addSubview:self.imagesView];
    [self.contentView addSubview:self.locationImageView];
    [self.contentView addSubview:self.locationLabel];
    [self.contentView addSubview:self.commentBtn];
    [self.contentView addSubview:self.praiseBtn];
}



- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@40);
        make.top.equalTo(@10);
    }];
    
    if(self.isShortLine) {
        // 短线
        [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.timeLabel.mas_centerY);
            make.left.equalTo(@20);
            make.bottom.equalTo(@0);
            make.width.equalTo(@1);
        }];
    } else {
        // 满线
        [self.lineView mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(@0);
            make.left.equalTo(@20);
            make.bottom.equalTo(@0);
            make.width.equalTo(@1);
        }];
    }
    
    
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@40);
        make.right.equalTo(@(-20));
        make.top.mas_equalTo(self.timeLabel.mas_bottom).offset(15);
    }];
    
    [self.imagesView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(@40);
        make.top.equalTo(self.contentLabel.mas_bottom).offset(3);
        make.height.equalTo(@(self.imageWidth));
    }];
    
    [self.praiseBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.imagesView.mas_bottom).offset(5);
        make.right.equalTo(@(-10));
        make.size.mas_equalTo(CGSizeMake(60, 40));
    }];
    
    [self.commentBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.praiseBtn.mas_left).offset(-5);
        make.centerY.equalTo(self.praiseBtn.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(60, 40));
    }];
    
    [self.locationImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@40);
        make.centerY.equalTo(self.praiseBtn.mas_centerY);
        make.size.mas_equalTo(CGSizeMake(10, 10));
    }];
    
    [self.locationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.locationImageView.mas_right).offset(3);
        make.centerY.equalTo(self.praiseBtn.mas_centerY);
    }];
    
    if(self.dateStr && ![self.dateStr isEqual:[NSNull null]] && self.dateStr.length > 0) {
        // 线条上的icon
        [self.lineImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.lineView.mas_centerX);
            make.centerY.equalTo(self.timeLabel.mas_centerY);
            make.size.mas_equalTo(CGSizeMake(15, 15));
        }];
    }
}

- (void)createImage {
    
}

#pragma mark - action
- (void)commentBtnClick {
    
}

- (void)praiseBtnClick:(UIButton *)btn {
    btn.selected = !btn.selected;
}

#pragma mark - Getters/Setters
- (void)setModel:(WritingRespDTOListItem *)model {
    _model = model;
    
    // 清除图片
    for (int tag = 1000; tag <= 1002; tag++) {
        [[self.contentView viewWithTag:tag] removeFromSuperview];
    }
    
    if(model) {
        self.contentLabel.text = model.title;
        [self.commentBtn setTitle:[NSString stringWithFormat:@"%ld",model.commentNum] forState:UIControlStateNormal];
        self.praiseBtn.selected = model.praiseState;
        self.locationLabel.text = model.location;
        self.images = model.images;
    }
    
    [self layoutIfNeeded];
}

- (void)setIsShortLine:(BOOL)isShortLine {
    _isShortLine = isShortLine;
    [self layoutIfNeeded];
}

- (void)setDateStr:(NSString *)dateStr {
    _dateStr = dateStr;
    if(dateStr && ![dateStr isEqual:[NSNull null]]) {
        self.timeLabel.text = dateStr;
        if(dateStr.length == 0) {
            // 不显示日期的情况
//            [self.timeLabel mas_updateConstraints:^(MASConstraintMaker *make) {
//                make.height.equalTo(@(CGFLOAT_MIN));
//            }];
            [self.lineImageView removeFromSuperview];
        } else {
            // 显示日期的情况,添加线条上的icon
            [self.contentView addSubview:self.lineImageView];
        }
    }
}

- (void)setImages:(NSArray<NSString *> *)images {
    if([images isKindOfClass:[NSArray class]]) {
        switch (images.count) {
            case 0:
            {
                self.imageWidth = CGFLOAT_MIN;
            }
                break;
                
            case 1:
            {
                self.imageWidth = kScreenWidth/2; // 一张图片时的图片宽度
                UIButton *imageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
                imageBtn.tag = 1000;
                imageBtn.frame = CGRectMake(5, 0, self.imageWidth, self.imageWidth);
                [imageBtn setBackgroundImage:[UIImage imageNamed:@"1.jpg"] forState:UIControlStateNormal];
                [self.imagesView addSubview:imageBtn];
            }
                break;
                
            case 2:
            {
                self.imageWidth = (kScreenWidth-60)/2; // 2张图片时的图片宽度
                UIButton *imageBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
                imageBtn1.tag = 1000;
                imageBtn1.frame = CGRectMake(5, 0, self.imageWidth, self.imageWidth);
                [imageBtn1 setBackgroundImage:[UIImage imageNamed:@"1.jpg"] forState:UIControlStateNormal];
                [self.imagesView addSubview:imageBtn1];
                
                UIButton *imageBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
                imageBtn2.tag = 1001;
                imageBtn2.frame = CGRectMake(CGRectGetMaxX(imageBtn1.frame)+3, 0, self.imageWidth, self.imageWidth);
                [imageBtn2 setBackgroundImage:[UIImage imageNamed:@"1.jpg"] forState:UIControlStateNormal];
                [self.imagesView addSubview:imageBtn2];
            }
                break;
                
            case 3:
            {
                self.imageWidth = (kScreenWidth-40-3-3-10)/3; // 3张图片时的图片宽度
                UIButton *imageBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
                imageBtn1.tag = 1000;
                imageBtn1.frame = CGRectMake(5, 0, self.imageWidth, self.imageWidth);
                [imageBtn1 setBackgroundImage:[UIImage imageNamed:@"1.jpg"] forState:UIControlStateNormal];
                [self.imagesView addSubview:imageBtn1];
                
                UIButton *imageBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
                imageBtn2.tag = 1001;
                imageBtn2.frame = CGRectMake(CGRectGetMaxX(imageBtn1.frame)+3, 0, self.imageWidth, self.imageWidth);
                [imageBtn2 setBackgroundImage:[UIImage imageNamed:@"1.jpg"] forState:UIControlStateNormal];
                [self.imagesView addSubview:imageBtn2];
                
                UIButton *imageBtn3 = [UIButton buttonWithType:UIButtonTypeCustom];
                imageBtn3.tag = 1002;
                imageBtn3.frame = CGRectMake(CGRectGetMaxX(imageBtn2.frame)+3, 0, self.imageWidth, self.imageWidth);
                [imageBtn3 setBackgroundImage:[UIImage imageNamed:@"1.jpg"] forState:UIControlStateNormal];
                [self.imagesView addSubview:imageBtn3];
            }
                break;
                
            default:
                break;
        }
    }
    _images = images;
}

- (UILabel *)timeLabel {
    if(!_timeLabel) {
        _timeLabel = [UILabel new];
        _timeLabel.font = [UIFont systemFontOfSize:20];
    }
    return _timeLabel;
}

- (UILabel *)contentLabel {
    if(!_contentLabel) {
        _contentLabel = [UILabel new];
        _contentLabel.numberOfLines = 0;
        _contentLabel.font = [UIFont systemFontOfSize:16];
    }
    return _contentLabel;
}

- (UILabel *)locationLabel {
    if(!_locationLabel) {
        _locationLabel = [UILabel new];
        _locationLabel.font = [UIFont systemFontOfSize:12];
    }
    return _locationLabel;
}

- (UIImageView *)locationImageView {
    if(!_locationImageView) {
        _locationImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"location_logo"]];
    }
    return _locationImageView;
}

- (UIButton *)commentBtn {
    if(!_commentBtn) {
        _commentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _commentBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _commentBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [_commentBtn setTitle:@"0" forState:UIControlStateNormal];
        [_commentBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_commentBtn setImage:[UIImage imageNamed:@"heart_unselected"] forState:UIControlStateNormal];
        _commentBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 3, 0, 0);
        _commentBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 3);
        [_commentBtn addTarget:self action:@selector(commentBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _commentBtn;
}

- (UIButton *)praiseBtn {
    if(!_praiseBtn) {
        _praiseBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _praiseBtn.titleLabel.font = [UIFont systemFontOfSize:12];
        [_praiseBtn setTitle:@"点赞" forState:UIControlStateNormal];
        [_praiseBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_praiseBtn setImage:[UIImage imageNamed:@"heart_unselected"] forState:UIControlStateNormal];
        [_praiseBtn setImage:[UIImage imageNamed:@"heart_selected"] forState:UIControlStateSelected];
        _praiseBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 3, 0, 0);
        _praiseBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 3);
        [_praiseBtn addTarget:self action:@selector(praiseBtnClick:) forControlEvents:UIControlEventTouchUpInside];

    }
    return _praiseBtn;
}

- (UIView *)imagesView {
    if(!_imagesView) {
        _imagesView = [UIView new];
    }
    return _imagesView;
}

- (UIView *)lineView {
    if(!_lineView) {
        _lineView = [UIView new];
        _lineView.backgroundColor = [UIColor lightGrayColor];
    }
    return _lineView;
}

- (UIImageView *)lineImageView {
    if(!_lineImageView) {
        _lineImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"clock"]];
    }
    return _lineImageView;
}

@end
